#include "../../source/http/http.hpp"
#include <assert.h>

void TestSplit()
{
    std::string src = ",,,,abc,,,bdc,,";
    std::vector<std::string> array;
    Util::Split(src, ",", &array); 

    for (auto a : array) {
        std::cout << "[" << a << "]" << std::endl;
    }
}

void TestReadFile() {
    // 创建一个临时测试文件
    std::string test_file = "test_readfile.tmp";
    std::string test_content = "Hello, World!\nThis is a test file.";

    {
        std::ofstream ofs(test_file);
        if (!ofs) {
            std::cerr << "Failed to create test file: " << test_file << std::endl;
            return ;
        }
        ofs << test_content;
    }

    // 测试1: 正常读取文件内容
    {
        std::string content;
        bool success = Util::ReadFile(test_file, &content);
        assert(success == true);
        assert(content == test_content);
        std::cout << "Test 1 passed: ReadFile successfully read correct content." << std::endl;
    }

    // 测试2: 文件不存在
    {
        std::string content;
        bool success = Util::ReadFile("non_existent_file.txt", &content);
        assert(success == false);
        std::cout << "Test 2 passed: File not found handled correctly." << std::endl;
    }

    // 测试3: 读取空文件
    {
        const std::string empty_file = "empty_file.tmp";
        std::ofstream ofs(empty_file); // 创建空文件
        std::string content;
        bool success = Util::ReadFile(empty_file, &content);
        assert(success == true);
        assert(content.empty());
        std::cout << "Test 3 passed: Empty file handled correctly." << std::endl;

        // 删除空文件
        std::remove(empty_file.c_str());
    }

    // 删除测试文件
    std::remove(test_file.c_str());

    std::cout << "All tests passed successfully!" << std::endl;
}

void TestWriteFile()
{
    // std::string buf = "IsLand1314";
    // bool ret = Util::WriteFile("./tmp.txt", buf);
    // if(!ret) return;
    // std::cout << buf << std::endl;
    
    std::string buf;
    if(!Util::ReadFile("../../example/eventfd/eventfd.c", &buf)){
        std::cerr << "ReadFile ERROR" << std::endl;
        return ;
    }

    if(!Util::WriteFile("./tmp.c", buf)){
        std::cerr << "WriteFile ERROR" << std::endl;
        return ;
    }

}


void Testcode()
{
    // std::string str = "+";
    // std::string res;
    // for(auto &c: str){
    //     char tmp[4] = {0};
    //     snprintf(tmp, 4, "%%%X", c);
    //     std::cout << tmp << std::endl;
    // }

    // std::string str = "C++  ";
    // std::string res = Util::UrlEncode(str, false);
    // std::cout << res << std::endl;


    // 测试 UrlEncode
    assert(Util::UrlEncode("Hello World", true) == "Hello+World");
    assert(Util::UrlEncode("Hello World", false) == "Hello%20World");
    assert(Util::UrlEncode("abc123.-_~!*()", true) == "abc123.-_~!*()");
    assert(Util::UrlEncode("测试", true) == "%E6%B5%8B%E8%AF%95"); // UTF-8 编码

    // 测试 UrlDecode
    assert(Util::UrlDecode("Hello+World", true) == "Hello World");
    assert(Util::UrlDecode("Hello%20World", false) == "Hello World");
    assert(Util::UrlDecode("%E6%B5%8B%E8%AF%95", true) == "测试");
    
    std::cout << Util::UrlDecode("%GZ", true) << std::endl;
//    assert(Util::UrlDecode("%GZ", true) == "%GZ"); // 非法编码

    assert(Util::UrlDecode("%1", true) == "%1");   // 不完整编码
    std::cout << Util::UrlDecode("%2b", true) << std::endl;
    std::cout << "所有测试通过！" << std::endl;

    std::string res = Util::UrlEncode("%GZ", true);
    std::string ret = Util::UrlDecode(res, true);
    std::cout << res << " " << ret << std::endl;
}

void TestExt(){
    std::cout << Util::ExtMime("a.txt") << std::endl;
}

void Testfile()
{
    std::cout << Util::IsDirectory("regex.cpp") << std::endl;   
    std::cout << Util::IsDirectory("regex") << std::endl;   

    std::cout << Util::IsRegular("regex") << std::endl;   
    std::cout << Util::IsRegular("regex.cpp") << std::endl;  
}

void TestPath()
{
    std::cout << Util::ValidPath("/html/index.html") << std::endl;
    std::cout << Util::ValidPath("../html/index.html") << std::endl;
    std::cout << Util::ValidPath("./html/index.html") << std::endl;
}


int main()
{
    
    return 0;
}